﻿namespace MapleRock.P2PEmulator.Core
{
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Net;
  using System.Text;

  public sealed class City
  {
    public City(
      uint id, 
      double latitude,
      double longitude,
      double radius)
    {
      Debug.Assert(id > 0, "id > 0");
      this.Id = id;
      this.Latitude = latitude;
      this.Longitude = longitude;
      this.Radius = radius;
      this.Size = 0;
    }

    public uint Id { get; private set; }

    public double Latitude { get; private set; }

    public double Longitude { get; private set; }

    public double Radius { get; private set; }

    public int Size { get; private set; }

    public double GetDistance(City rhs)
    { 
      var latitudeDelta = this.Latitude - rhs.Latitude;
      var longitudeDelta = this.Longitude - rhs.Longitude;

      latitudeDelta *= latitudeDelta;
      longitudeDelta *= longitudeDelta;
      return Math.Sqrt(latitudeDelta + longitudeDelta);
    }

    public double GetSignalSpreadSpanMs(City rhs)
    {
      var random = new Random();

      var aDistance = this.GetDistance(rhs) + (random.NextDouble() * this.GetDistance(rhs));
      var bDistance = this.GetDistance(rhs) + (random.NextDouble() * this.GetDistance(rhs));
      var cDistance = this.GetDistance(rhs) + (random.NextDouble() * this.GetDistance(rhs));

      var a = LatencyTable.GetLatency(aDistance);
      var b = LatencyTable.GetLatency(bDistance);
      var c = LatencyTable.GetLatency(cDistance);

      return (a + b + c) / 3;
    }

    public void InitSize(int size)
    {
      Debug.Assert(0 == this.Size, "0 == this.Size");
      Debug.Assert(size > 0, "size > 0");

      this.Size = size;
    }
  }
}
